package com.wetao.testrxjava.dagger;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.wetao.testrxjava.R;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;

import dagger.Lazy;

//https://blog.csdn.net/WOL965/article/details/80777201   这篇博客比较好
public class Dagger1Activity extends AppCompatActivity {
    //@Inject注解的字段不能是private和protected的.
    @Inject
    Cloth cloth;

    @Inject
    Shoe shoe;

    @Inject
    Clothes clothes;

    @Inject
    @Named("red")
    Cloth2 redCloth;
    @Inject
    @Named("blue")
    Cloth2 blueCloth;
    @Inject
    @RedCloth
    Cloth2 redCloth_q;

    @Inject
    Clothes2 clothes2;

    @Inject
    Cloth3 cloth3;

    @Inject
    Cloth3 cloth3_2;

    @Inject
    Cloth4 cloth4;

    @Inject
    Cloth4 cloth4_2;


    //需要注意的是,使用Lazy和Provider时,与该依赖对象有关的Module实例会被Lazy和Provider引用,
    // 所以该Module实例不会被垃圾回收器回收
    @Inject
    Lazy<Cloth> redCloth_lazy;
    @Inject
    Provider<Shoe> shoe_provider;


    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dagger1);
        tv = findViewById(R.id.tv);

        Dagger1ActivityComponent build = DaggerDagger1ActivityComponent.builder().dagger1ActivityModule(new Dagger1ActivityModule()).build();
        build.inject(this);
        tv.setText("我现在有" + cloth + "和" + shoe
                + "和" + clothes + "和" + redCloth
                + "和" + blueCloth + "和" + clothes2
                + "和" + redCloth_q
                + "和" + (cloth == clothes.getCloth() ? "true" : "false") // 两个对象不相等，都会创建一个新对象
                + "和" + (cloth3 == cloth3_2 ? "true" : "false") // @single，所有相等
                + "和" + (cloth4 == cloth4_2 ? "true" : "false") // @scope和@single一样，所有相等
        );

        findViewById(R.id.btn_dagger2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Dagger1Activity.this, Dagger2Activity.class));
            }
        });


    }
}
